package com.woniu.service.impl;

import com.woniu.entity.InpatientDoctorOrder;
import com.woniu.mapper.InpatientDoctorOrderMapper;
import com.woniu.service.IDoctorOrderService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.utils.UserContext;
import com.woniu.vo.DoctorOrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Service
@Transactional
public class IDoctorOrderServiceImpl implements IDoctorOrderService {
    @Resource
    private InpatientDoctorOrderMapper mapper;
    @Override
    public ResponseUtil listDoctorOrders(Integer patientId) {
        List<DoctorOrderVO> list = mapper.listDoctorOrders(patientId);
        return list.size() > 0 ?
                ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil addRecord(InpatientDoctorOrder inpatientDoctorOrder) throws ParseException {
//        inpatientDoctorOrder.setDoWorkerId(1);
        inpatientDoctorOrder.setDoWorkerId(UserContext.ContextUserId());
        //从后端定义医嘱的状态
        inpatientDoctorOrder.setDoStatus(0);
        Integer result = mapper.insertSelective(inpatientDoctorOrder);
        return result > 0 ?
                ResponseUtil.get(ResponseEnum.OK,inpatientDoctorOrder.getId())
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil updateRecord(InpatientDoctorOrder inpatientDoctorOrder) throws ParseException {
//        inpatientDoctorOrder.setDoDoworkerId(1);
        inpatientDoctorOrder.setDoDoworkerId(UserContext.ContextUserId());
        Integer result = mapper.updateByPrimaryKeySelective(inpatientDoctorOrder);
        return result > 0 ?
                ResponseUtil.get(ResponseEnum.OK,result)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
}
